package com.appistry.maven.plugin.far.remote.ssh;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;

public class SshFtpIT {

	private static final String TEMP_DEST_TEST_DIR = "tempDestTestDir";
	private static final String CHILD_TEMP_TEST_DIR = "childTempDestTestDir";
	private static final String TEMP_SOURCE_DIR_NAME = "tempSourceTestDir";
	private File sourceDir;

	private SshFtp sshFtp;
	private SshExec sshExec; // run ssh commands to check files were transferred

	@Before
	public void setUp() throws Exception {

		sourceDir = new File(TEMP_SOURCE_DIR_NAME);
		if (sourceDir.exists() && !sourceDir.isDirectory()) {
			throw new Exception("Cannot create temp directory");
		}
		sourceDir.mkdir();

		SshConfig sshConfig = new SshConfig(SshConstants.HOST,
				SshConstants.PORT, new SshUserInfo(SshConstants.USER,
						SshConstants.PASSWORD, true));

		sshFtp = new SshFtp(sshConfig);

		sshExec = new SshExec(sshConfig);
		sshExec.executeCommand("mkdir " + TEMP_DEST_TEST_DIR);

	}

	@After
	public void tearDown() throws Exception {
		if (sourceDir.exists()) {
			org.apache.commons.io.FileUtils.forceDelete(sourceDir);
		}

		// this is dangerous. But is necessary for cleanup
		sshExec.executeCommand("rm -rf " + TEMP_DEST_TEST_DIR);
		sshExec = null;

		sshFtp = null;
	}

	@Test
	public void testExecuteCommand() throws IOException, JSchException,
			SftpException {
		// create source temp files
		File tempFile = File.createTempFile("temp", "test", sourceDir);
		// System.out.println("Temp file: " + tempFile.getName());

		sshFtp.executeCommand(tempFile.getAbsolutePath(), TEMP_DEST_TEST_DIR
				+ File.separator + tempFile.getName(), "put");

		sshExec.executeCommand("ls " + TEMP_DEST_TEST_DIR + File.separator
				+ tempFile.getName());
		assertEquals(TEMP_DEST_TEST_DIR + File.separator + tempFile.getName(),
				sshExec.getResult().trim());

	}

	@Test
	public void testExecuteCommandCreateDestDir() throws IOException,
			JSchException, SftpException {
		// create source temp files
		File tempFile = File.createTempFile("temp", "test", sourceDir);
		// System.out.println("Temp file: " + tempFile.getName());

		// setup child temp test dir
		sshExec.executeCommand("mkdir -p " + TEMP_DEST_TEST_DIR
				+ File.separator + CHILD_TEMP_TEST_DIR);

		sshFtp.executeCommand(tempFile.getAbsolutePath(), TEMP_DEST_TEST_DIR
				+ File.separator + CHILD_TEMP_TEST_DIR + File.separator
				+ tempFile.getName(), "put");

		// List<String> commands = new ArrayList<String>();
		// commands.add("cd rosh" + File.separator + TEMP_DEST_TEST_DIR);
		// commands.add("ls");
		sshExec.executeCommand("cd " + TEMP_DEST_TEST_DIR + File.separator
				+ CHILD_TEMP_TEST_DIR + "; ls");
		assertEquals(tempFile.getName(), sshExec.getResult().trim());

		// cleanup child temp test dir
		sshExec.executeCommand("rm -rf " + TEMP_DEST_TEST_DIR + File.separator
				+ CHILD_TEMP_TEST_DIR);
	}

	@Ignore
	@Test
	public void testSFtpLargeFile() throws JSchException, SftpException {
		File largeFile = new File("target" + File.separator + "classes",
				"largefile.zip");
		sshFtp.executeCommand(largeFile.getAbsolutePath(), TEMP_DEST_TEST_DIR
				+ File.separator + largeFile.getName(), "put");

		sshExec.executeCommand("ls " + TEMP_DEST_TEST_DIR + File.separator
				+ largeFile.getName());
		assertEquals(TEMP_DEST_TEST_DIR + File.separator + largeFile.getName(),
				sshExec.getResult().trim());
	}
}
